package org.example.lotterysystem.dao.mapper;

import org.apache.ibatis.annotations.*;
import org.example.lotterysystem.dao.dataobject.DeadLetterMessageDO;

import java.util.List;

@Mapper
public interface DeadLetterMessageMapper {

    @Insert("INSERT INTO dead_letter_message(exchange, routing_key, message_content, create_time, status) " +
            "VALUES(#{exchange}, #{routingKey}, #{messageContent}, NOW(), #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(DeadLetterMessageDO entity);

    @Update("UPDATE dead_letter_message SET status = #{status}, retry_count = #{retryCount}, " +
            "last_retry_time = #{lastRetryTime}, error_msg = #{errorMsg} WHERE id = #{id}")
    int updateById(DeadLetterMessageDO entity);

    @Select("SELECT * FROM dead_letter_message WHERE status = 0 ORDER BY create_time ASC LIMIT #{limit}")
    List<DeadLetterMessageDO> selectPendingMessages(@Param("limit") int limit);

    @Select("SELECT * FROM dead_letter_message WHERE id = #{id}")
    DeadLetterMessageDO selectById(long id);
}